package com.dailyblue.example.springcloud.seata.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.example.springcloud.commons.util.JsonResult;
import com.dailyblue.example.springcloud.commons.util.ResultTool;
import com.dailyblue.example.springcloud.seata.order.bean.Order;
import com.dailyblue.example.springcloud.seata.order.feign.StockFeign;
import com.dailyblue.example.springcloud.seata.order.mapper.OrderMapper;
import com.dailyblue.example.springcloud.seata.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private StockFeign stockFeign;

    @GlobalTransactional
    @SneakyThrows
    public JsonResult saveOrder(Order order) {
        order.setId(IdWorker.getId() + "");
        boolean b = save(order);
        if (!b) {
            throw new SQLException();
        }
        stockFeign.update(order.getGid(), order.getNumber());
        return ResultTool.success("success");
    }
}
